package top.chendawei.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.chendawei.util.params.ResultMessage;

import javax.servlet.http.HttpServletRequest;

public class CommonUtils {
    private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);
    private static String injector;
    private static boolean isLogicDelete;

    public CommonUtils() {
    }


    public static HttpServletRequest getHttpRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        return requestAttributes != null ? ((ServletRequestAttributes) requestAttributes).getRequest() : null;
    }

    public static String getRequestHeader(String header) {
        HttpServletRequest request = getHttpRequest();
        return request != null ? request.getHeader(header) : null;
    }

    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if (ip != null && ip.contains(",")) {
            String[] ips = ip.split(",");
            if (ips.length > 0) {
                ip = ips[0];
            }
        }

        return ip;
    }


    public static String toUnderLineName(String value) {
        if (StringUtils.isEmpty(value)) {
            return "";
        } else {
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < value.length(); ++i) {
                String s = value.substring(i, i + 1);
                if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0)) && !"_".equals(s)) {
                    result.append("_");
                    s = s.toLowerCase();
                }

                result.append(s);
            }

            return result.toString();
        }
    }

    public static String camelName(String name) {
        StringBuilder result = new StringBuilder();
        if (name != null && !name.isEmpty()) {
            if (!name.contains("_")) {
                return name.substring(0, 1).toLowerCase() + name.substring(1);
            } else {
                String[] camels = name.split("_");
                String[] var3 = camels;
                int var4 = camels.length;

                for (int var5 = 0; var5 < var4; ++var5) {
                    String camel = var3[var5];
                    if (!camel.isEmpty()) {
                        if (result.length() == 0) {
                            result.append(camel.toLowerCase());
                        } else {
                            result.append(camel.substring(0, 1).toUpperCase());
                            result.append(camel.substring(1).toLowerCase());
                        }
                    }
                }

                return result.toString();
            }
        } else {
            return "";
        }
    }

    public static Throwable loadCause(Throwable e) {
        Throwable result = e;
        if (e != null) {
            while (result.getCause() != null) {
                result = result.getCause();
            }
        }

        return result;
    }

    public static String loadCauseMsg(Throwable e) {
        String msg = loadCause(e).getMessage();
        return msg != null ? msg : e.getMessage();
    }

    public static <T> T extractResult(ResultMessage<T> result, String msg) {
        if (result != null && result.getCode() == 200) {
            return result.getData();
        } else {
            throw new RuntimeException(msg);
        }
    }

    public static <T> T extra(ResultMessage<T> result) {
        return extractResult(result, "调用服务失败");
    }
}
